from django.shortcuts import render
from rest_framework.generics import ListAPIView
from rest_framework.views import APIView
from .serializers import SKUSerializer
from .models import SKU
from rest_framework.filters import OrderingFilter
from drf_haystack.viewsets import HaystackViewSet
from .serializers import SKUIndexSerializer
# Create your views here.
class SKUListView(ListAPIView):
    serializer_class = SKUSerializer
    # queryset = SKU.object.all()
    # 排序后端
    filter_backends = [OrderingFilter]
    # 排序字段
    ordering_fields = ['create_time', 'price', 'sales']
    def get_queryset(self):
        category_id = self.kwargs.get('category_id')
        # 指定分类
        return SKU.objects.filter(category_id=category_id, is_launched=True)


class SKUSearchViewSet(HaystackViewSet):
    """
    SKU搜索
    """
    index_models = [SKU]

    serializer_class = SKUIndexSerializer